package com.boyon.customer.service.customer.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.boyon.common.exection.BusinessException;
import com.boyon.common.status.StatusType;
import com.boyon.common.tools.ObjectTool;
import com.boyon.customer.domain.CustomerData;
import com.boyon.customer.entity.Customer;
import com.boyon.customer.entity.Finace;
import com.boyon.customer.service.customer.CustomerServiceInterface;
import com.boyon.hbm.criteria.OperationType;
import com.boyon.hbm.criteria.Param;
import com.boyon.hbm.criteria.Query;
import com.boyon.service.AbstractService;

/**
 * ClassName:CustomerService <br/>
 * Function: 客户资源类. <br/>
 * Date: 2018年4月2日 下午9:53:26 <br/>
 * 
 * @author sunshulin
 * @version
 * @since JDK 1.8
 * @see
 */
@Service
public class CustomerService extends AbstractService
		implements CustomerServiceInterface {

	public Integer getType() {
		return -1;
	}

	/**
	 * 
	 * save:新增客户档案信息. <br/>
	 * 
	 * @author sunshulin
	 * @param customerData
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional
	public Long save(CustomerData customerData) {
		Long customerId = saveOrUpdate(customerData);
		return customerId;
	}

	/**
	 * 
	 * editor:修改客户档案.
	 * 
	 * @see com.boyon.customer.service.customer.CustomerServiceInterface#editor(com.boyon.customer.domain.CustomerData)
	 */
	@Transactional
	public Long editor(CustomerData customerData) {
		Long customerId = saveOrUpdate(customerData,
				new String[] { "customerNo", "customerId", "status", "customerCategory" });
		return customerId;
	}

	/**
	 * 
	 * saveOrUpdate:新增或修改客户档案资料. <br/>
	 * 
	 * @author sunshulin
	 * @param customerData
	 * @return
	 * @since JDK 1.8
	 */
	private Long saveOrUpdate(CustomerData customerData, String... strings) {
		Customer customer = null;
		if (customerData.getCustomerId() != null) {
			customer = generalDao.get(Customer.class, customerData.getCustomerId());
		} else {			
			customer = new Customer();
		}
		ObjectTool.copy(customerData, customer, strings);
		customer.setStatus(StatusType.start.getStatus());
		generalDao.saveOrUpdate(customer);
		Finace finace = new Finace();
		ObjectTool.copy(customerData, finace);
		finace.setCustomerId(customer.getCustomerId());
		generalDao.saveOrUpdate(finace);
		return customer.getCustomerId();
	}

	/**
	 * 
	 * 删除客户档案.
	 * 
	 * @see com.boyon.customer.service.customer.CustomerServiceInterface#delete(java.lang.Long)
	 */
	@Transactional
	public Long delete(Long customerId) {
		Customer customer = getCustomerByCustomerId(customerId);
		customer.setStatus(StatusType.del.getStatus());
		generalDao.saveOrUpdate(customer);
		return customer.getCustomerId();
	}

	/**
	 * 
	 * 查找客户档案.
	 * 
	 * @see com.boyon.customer.service.customer.CustomerServiceInterface#findByCustomerId(java.lang.Long)
	 */
	@Transactional(readOnly = true)
	public CustomerData findByCustomerId(Long customerId) {
		Customer customer = getCustomerByCustomerId(customerId);
		CustomerData customerData = new CustomerData();
		ObjectTool.copy(customer, customerData);
		return customerData;
	}

	/**
	 * 
	 * getByCustomerId:查找客户档案信息. <br/>
	 * 
	 * @author sunshulin
	 * @param customerId
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional(readOnly = true)
	public Customer getCustomerByCustomerId(Long customerId) {
		Customer customer = generalDao.get(Customer.class, customerId);
		if (customer == null) {
			throw BusinessException.builder(0, "客户档案信息不存在");
		}
		if (customer.getStatus() == StatusType.del.getStatus()) {
			throw BusinessException.builder(0, "客户档案信息已删除");
		}
		return customer;
	}

	/**
	 * 
	 * getByCustomerId:查找客户信用信息. <br/>
	 * 
	 * @author sunshulin
	 * @param customerId
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional(readOnly = true)
	public Finace getFinaceByCustomerId(Long customerId) {
		Finace finace = generalDao.get(Finace.class, customerId);
		if (finace == null) {
			throw BusinessException.builder(0, "客户档案信用信息不存在");
		}
		return finace;
	}

	/**
	 * 
	 * 查找客户档案.
	 * 
	 * @see com.boyon.customer.service.customer.CustomerServiceInterface#query(com.boyon.customer.domain.CustomerData)
	 */
	@Transactional(readOnly = true)
	public List<CustomerData> query(CustomerData data, Query query) {
		List<CustomerData> datas = new ArrayList<>();
		query.addParam(new Param<Integer>("status", OperationType.nq,
				StatusType.del.getStatus()));
		List<Customer> list = generalDao.query(Customer.class, query);
		for (Customer customer : list) {
			CustomerData customerData = new CustomerData();
			ObjectTool.copy(customer, customerData);
			Finace finace = getFinaceByCustomerId(customer.getCustomerId());
			ObjectTool.copy(finace, customerData);
			datas.add(customerData);
		}
		return datas;
	}

	/**
	 * 
	 * addParam:查询条件. <br/>
	 * 
	 * @author sunshulin
	 * @param query
	 * @param customerData
	 * @since JDK 1.8
	 */
	public void addParam(Query query, CustomerData customerData) {
		query.addParam(new Param<String>("customerName", OperationType.like,
				customerData.getCustomerName()));
		query.addParam(new Param<String>("customerNo", OperationType.like,
				customerData.getCustomerNo()));
		query.addParam(new Param<Integer>("customerType", OperationType.eq,
				customerData.getCustomerType()));
	}

	/**
	 * 重置账号密码.
	 * 
	 * @see com.boyon.customer.service.customer.CustomerServiceInterface#resetPass(java.lang.Long)
	 */
	@Transactional(readOnly = true)
	public void resetPass(Long customerId) {
		
	}
}
